package cate.game.activity.firstpay;

import cate.common.table.activity.firstpay.common.SuperFirstPayCommonRow;
import cate.common.table.activity.firstpay.item.SuperFirstPayItemRow;
import cate.common.table.d.GDFunc;
import cate.game.activity.firstpay.msg.SuperFirstPayInfoResp;
import cate.game.role.FuncOpenChecker;
import cate.game.role.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.beans.Transient;
import java.util.HashMap;
import java.util.Map;

@NoteClass(value = "超值首充")
public class SuperFirstPayPO extends FuncOpenChecker {
    @NoteField(value = "所有档位")
    public Map<Integer, SuperFirstPayLevel> levels;

    @Override
    public int getFuncId() {
        return GDFunc.ACT_SUPER_FIRST_PAY;
    }

    @Override
    public void onAfterOpen() {
        onPay();
    }

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (levels == null) {
            levels = new HashMap<>();
        }
        for (SuperFirstPayCommonRow configRow : role.getGame().table.firstPay.common.getList()) {
            if (!levels.containsKey(configRow.id)) {
                levels.put(configRow.id, new SuperFirstPayLevel(configRow.id));
            }
        }
        levels.values().forEach(levelPO -> levelPO.initialize(role));
        onPay();
    }

    @Override
    public void onDaySpan(boolean silence) {
        levels.values().forEach(levelPO -> levelPO.onDaySpan(silence));
        noticeUpdate();
    }

    /**
     * 同步信息到前端
     */
    public void noticeUpdate() {
        role.sendNow(new SuperFirstPayInfoResp(role.getPay().paySum, this));
    }

    /**
     * 充值时触发
     */
    public void onPay() {
        levels.values().forEach(SuperFirstPayLevel::onPay);
        noticeUpdate();
    }

    /**
     * 领取奖励
     *
     * @param configId 配置ID
     * @return 领取结果 无携带数据
     */
    @JsonIgnore
    @Transient
    public EcResult<Void> getReward(int configId) {
        EcResult<Void> r = new EcResult<>();
        SuperFirstPayItemRow configRow = role.getGame().table.firstPay.item.get(configId);
        if (configRow == null || !levels.containsKey(configRow.level)) {
            return r.fail("找不到配置");
        }
        return levels.get(configRow.level).getReward(configId);
    }

    /**
     * 领取奖励
     *
     * @param level 挡位
     * @return 领取结果 无携带数据
     */
    @JsonIgnore
    @Transient
    public EcResult<Void> getRewardByLevel(int level) {
        EcResult<Void> r = new EcResult<>();
        SuperFirstPayLevel payLevel = levels.get(level);
        if (payLevel == null) {

            return r.fail("挡位不存在");
        }
        return payLevel.getRewardByLevel();
    }
}
